package com.example.opengldemo.base;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import android.annotation.SuppressLint;
import android.opengl.GLES30;
import android.view.View;

import com.example.opengldemo.util.MatrixState;
import com.example.opengldemo.util.ShaderUtil;


@SuppressLint("NewApi") 
public class TextureRect 
{
	int mProgram;
    int muMVPMatrixHandle;
    int maPositionHandle;
    int maTexCoorHandle;
    String mVertexShader;
    String mFragmentShader;
	
	FloatBuffer   mVertexBuffer;
	FloatBuffer   mTexCoorBuffer;
    int vCount=0;
    
    public TextureRect(View mv, float ratio)
    {
    	initVertexData(ratio);
    	initShader(mv);
    }
    

    public void initVertexData(float ratio)
    {

        vCount=6;
        final float UNIT_SIZE=ratio;
        float vertices[]=new float[]
        {
        		-UNIT_SIZE,UNIT_SIZE,0, //-1 1  左上
        		-UNIT_SIZE,-UNIT_SIZE,0, // -1，-1 左下
        		UNIT_SIZE,-UNIT_SIZE,0, // 1 ，-1 右下
        		
        		UNIT_SIZE,-UNIT_SIZE,0, // 1，-1 右下
        		UNIT_SIZE,UNIT_SIZE,0, // 1，1 右上
        		-UNIT_SIZE,UNIT_SIZE,0 // -1，1 左上
        };
		


        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
        vbb.order(ByteOrder.nativeOrder());
        mVertexBuffer = vbb.asFloatBuffer();
        mVertexBuffer.put(vertices);
        mVertexBuffer.position(0);



        

        float texCoor[]=new float[]
        {    
        		0,1-0, 0,1-1.0f, 1.0f,1-1.0f,
        		1.0f,1-1.0f, 1.0f,1-0, 0,1-0  
        };        

        ByteBuffer cbb = ByteBuffer.allocateDirect(texCoor.length*4);
        cbb.order(ByteOrder.nativeOrder());
        mTexCoorBuffer = cbb.asFloatBuffer();
        mTexCoorBuffer.put(texCoor);
        mTexCoorBuffer.position(0);



    }


    public void initShader(View mv)
    {

        mVertexShader= ShaderUtil.loadFromAssetsFile("base/vertex_texture_vt.sh", mv.getResources());

        mFragmentShader=ShaderUtil.loadFromAssetsFile("base/frag_texture_vt.sh", mv.getResources());

        mProgram = ShaderUtil.createProgram(mVertexShader, mFragmentShader);

        maPositionHandle = GLES30.glGetAttribLocation(mProgram, "aPosition");

        maTexCoorHandle= GLES30.glGetAttribLocation(mProgram, "aTexCoor");

        muMVPMatrixHandle = GLES30.glGetUniformLocation(mProgram, "uMVPMatrix");  
    }
    
    public void drawSelf(int texId)
    {        

    	 GLES30.glUseProgram(mProgram);
    	 MatrixState.pushMatrix();

         MatrixState.translate(0, 0, 1);

         GLES30.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0); 

         GLES30.glVertexAttribPointer  
         (
         		maPositionHandle,   
         		3, 
         		GLES30.GL_FLOAT, 
         		false,
                3*4,   
                mVertexBuffer
         );       

         GLES30.glVertexAttribPointer  
         (
        		maTexCoorHandle, 
         		2, 
         		GLES30.GL_FLOAT, 
         		false,
                2*4,   
                mTexCoorBuffer
         );   

         GLES30.glEnableVertexAttribArray(maPositionHandle);

         GLES30.glEnableVertexAttribArray(maTexCoorHandle);  
         

         GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
         GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, texId);
         

         GLES30.glDrawArrays(GLES30.GL_TRIANGLES, 0, vCount); 
         MatrixState.popMatrix();
    }
}
